/**
 * Created by tangqiaojie <2482366539@qq.com> on 2020/4/12 0012
 */
const sendSbList = (currentZeroInfo, win)=>{
  /* let sbAllList=[
    {
      declareName:"增值税(适用于一般纳税人)",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"地方教育附加",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"教育费附加",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"城市维护建设税",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:true,
    },
    {
      declareName:"企业所得税(年报)",
      declareCycle:"year",
      accountBook:"",
      declareTime:"",
      isSuccess:false,
    },
    {
      declareName:"残疾人就业保障金",
      declareCycle:"year",
      accountBook:"",
      declareTime:"",
      isSuccess:false,
    }
  ]; */

  let sbAllList = getSbList(win);

  if ((window.sessionStorage.getItem('isCwbb'))) {
    sbAllList.push({
      declareName: '财务报表',
      declareCycle: 'month',
      accountBook: '',
      declareTime: '',
      isSuccess: true
    });
  }
  sbAllList.forEach((item, index)=>{
    const date = new Date(),
      year = date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate();
    if (item.declareCycle === 'month') {
      item.accountBook = moment().subtract(1, 'months').format('YYYY-MM');
    } else if (item.declareCycle === 'year') {
      item.accountBook = moment().subtract(1, 'years').format('YYYY');
    }
  });

  let backInfo = {
    ...currentZeroInfo,
    recordList: sbAllList
  };
  // 零申报结束
  window.postMessage(
    {
      event: 'zeroReportBack',
      data: backInfo
    },
    '*'
  );
};

const getSbList = (win)=>{
  let $ = win.$;
  return [].slice.call($('#gdslhsb tr')).map(item=>{
    let startTime = moment($('td:eq(3)', item).html()),
      endTime = moment($('td:eq(4)', item).html());
    if (startTime.isValid() && endTime.isValid()) {
      return {
        declareName: $('td:eq(1)', item).html(),
        declareCycle: moment($('td:eq(4)', item).html()).diff(moment($('td:eq(3)', item).html()), 'days') < 200 ? 'month' : 'year',
        accountBook: '',
        declareTime: '',
        isSuccess: $('.sbtnbox span', item).length > 0
      };
    }
  }).filter(item=>!!item);
};

const findIndexButton = (word, win = window)=>{
  let $ = win.$;
  let idx = [].slice.call($('#gdslhsb tr')).findIndex(item=>{
    return $('td:eq(1)', item).html() === word;
  });

  if (idx > -1) {
    let btn = $('#gdslhsb tr:eq(' + idx + ') .layui-btn'),
      text = $('#gdslhsb tr:eq(' + idx + ') .sbtnbox span'),
      refresh = $('#gdslhsb tr:eq(' + idx + ') .fsicon-refresh');
    if (btn.length) {
      return {
        txt: btn.html(),
        btn,
        refresh
      };
    } if (text.length) {
      return {
        txt: text.html(),
        btn: null
      };
    }
  } else {
    return {
      txt: '',
      btn: null
    };
  }
};


export {
  sendSbList,
  findIndexButton
};
